/**
 *
 */
package cn.fanyamin.dao.ibatis;


import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.fanyamin.dao.BookDao;
import cn.fanyamin.domain.Book;
import cn.fanyamin.domain.BookSearchCriteria;
import cn.fanyamin.domain.BorrowLog;
import cn.fanyamin.web.vo.BookVO;

/**
 *
 * @author walter
 */
public class SqlMapBookDao extends CRUDDao<Integer,Book> implements BookDao {
	private static final String CREATE_BORROWlOG_SQL = "createBorrowLog";
	private static final String RETRIEVE_BORROWlOG_SQL = "retrieveBorrowLog";
	private static final String UPDATE_BORROWlOG_SQL = "updateBorrowLog";
	private static final String DELETE_BORROWlOG_SQL = "deleteBorrowLog";
	private static final String DELETE_BORROWlOGS_SQL = "deleteBorrowLogs";
	private static final String LIST_BOOK_SQL = "listBook";

	
    protected String domainName = "Book";
    
    public SqlMapBookDao() {
        setSQLID(domainName);
    }

	public int createBook(Book book) {
		return super.create(book);
	}

	public int deleteBook(int id) {
		return super.delete(id);
	}

	public Book retrieveBook(int id) {
		return super.retrieve(id);
	}

	public int updateBook(Book t) {	
		return super.update(t);
	}

	public int createBorrowLog(BorrowLog borrowLog) {
		return (Integer)getSqlMapClientTemplate().insert(CREATE_BORROWlOG_SQL, borrowLog);
	}

	public int deleteBorrowLog(int id) {
		return getSqlMapClientTemplate().delete(DELETE_BORROWlOG_SQL, id);
	}

	public int deleteBorrowLogs(int bookID) {		
		return getSqlMapClientTemplate().delete(DELETE_BORROWlOGS_SQL, bookID);
	}

	public BorrowLog retrieveBorrowLog(int id) {		
		return (BorrowLog) getSqlMapClientTemplate().queryForObject(
				RETRIEVE_BORROWlOG_SQL, id);
	}

	public int updateBorrowLog(BorrowLog t) {
		return getSqlMapClientTemplate().update(UPDATE_BORROWlOG_SQL, t);
	}

	@SuppressWarnings("unchecked")
	public List<BookVO> getUserBooks(int userID) {

		return (List<BookVO>) getSqlMapClientTemplate().queryForList(
				"getUserBooks", userID);
	}

	@SuppressWarnings("unchecked")
	public List<BookVO> getAllBooks() {
		return (List<BookVO>) getSqlMapClientTemplate().queryForList(
				"getAllBooks", null);
	}

	@SuppressWarnings("unchecked")
	public List<BookVO> listBook(int pageNo, int pageSize,
			BookSearchCriteria criteria) {
		Map<String,Object> map = new HashMap<String,Object> ();
		map.put("pageNo", pageNo);
		map.put("pageSize", pageSize);
		map.put("criteria", criteria);
		
		return (List<BookVO>) getSqlMapClientTemplate().queryForList(
				LIST_BOOK_SQL, map);
	}

	@SuppressWarnings("unchecked")
	public List<BookVO> getUserBorrowLogs(int bookID) {
		return (List<BookVO>) getSqlMapClientTemplate().queryForList(
				"getUserBorrowLogs", bookID);
	}
	
	@SuppressWarnings("unchecked")
	public List<BookVO> getBookBorrowLogs(int bookID) {
		return (List<BookVO>) getSqlMapClientTemplate().queryForList(
				"getBookBorrowLogs", bookID);	}

	public int getBookCount(BookSearchCriteria criteria) {
		if(null == criteria) {
			return (Integer) getSqlMapClientTemplate().queryForObject(
					"getAllBookCount", criteria);
		}
		return (Integer) getSqlMapClientTemplate().queryForObject(
				"getBookCount", criteria);
	}
	
}